var skill = {
	//代码片段，根据id获取标签
	_getTag: function(tag_id) {
		var tags = this.tags || (this.tags = []);
		var index = tags.indexOf(tag_id);
		if (index === -1) {
			throwCusError("标签不属于指定商家");
		}
		var tag = TagCon.get(tag_id);
		if (!tag) {
			throwCusError("找不到指定标签")
		}
		this._getTag.index = index;
		return tag;
	},
	//获取标签
	//无需分页功能
	getTags: function() {
		var tags = (this.tags || []).slice();
		var result = [];
		var new_tags = [];
		tags.forEach(function(tag_id, index) {
			var tag = TagCon.get(tag_id);
			if (tag) {
				result.push(tag);
				new_tags.push(tag_id);
			}
		});
		if (new_tags.length !== tags.length) {
			this.tags = new_tags;
			this.save();
		}
		return result;
	},
	//详细信息，报考商品信息
	getTagsDetail: function(options) {
		var tags = this.getTags();
		var result = tags.map(function(tag) {
			return tag.getDetail(options);
		});
		return result;
	},
	getTagsDetailById: function(tag_id, options) {
		var tag = this._getTag(tag_id);
		console.log(tag_id, options);
		return tag.getDetail(options);
	},
	//增加标签
	addTag: function(tag_name) {
		if (!helper.folder_name(tag_name)) {
			throwCusError("标签名命名不规范")
		}
		var tag = new TagCon(tag_name, this._id);
		var tags = this.tags || (this.tags = []);
		var tag_id = tag._id
		tags.push(tag_id);
		this.save();
		return tag_id;
	},
	//移除标签
	removeTag: function(tag_id) {
		var tag = this._getTag(tag_id);
		var index = this._getTag.index;
		//标签的全部商品都要删除这个标签
		var tag_content = tag.content;
		tag_content.forEach(function(goods_id, index) {
			var goods = GoodsCon.get(goods_id);
			try {
				goods._removeTag(tag._id);
			} catch (e) {
				console.log(e);
			}
		});
		//标签内容为空后删除这个标签
		tag.destroy();
		//将标签从商家标签集合中删除
		var tags = this.tags || (this.tags = []);
		tags.splice(index, 1);
		this.save();
	},
	//修改标签名
	updateTagName: function(tag_id, new_tag_name) {
		var tag = this._getTag(tag_id);
		tag._rename(new_tag_name);
	},
	//改变标签的位置
	updateTagIndex: function(tag_id, tag_index) {
		var tags = this.tags || (this.tags = []);
		console.log(tags)
		var index = tags.indexOf(tag_id);
		if (index === -1) {
			throwCusError("标签不属于指定商家")
		}
		tags.splice(index, 1);
		tag_index = ~~tag_index;
		if (tag_index > tags.length) {
			tags.push(tag_id);
		} else if (tag_index < 0) {
			tags.unshift(tag_id);
		} else {
			tags.splice(tag_index, 0, tag_id)
		}
		this.tags = tags;
		this.save();
		return this;
	},
	//为商品添加标签
	addGoodsToTag: function(tag_id, goods_id) {
		//校验并获取商品
		var goods = this._getGoods(goods_id);
		//校验并获取标签
		var tag = this._getTag(tag_id);
		//商品添加标签
		goods._addTag(tag_id);
		tag._addGoods(goods_id);
	},
	//为商品移除标签
	removeGoodsFromTag: function(tag_id, goods_id) {
		//校验并获取商品
		var goods = this._getGoods(goods_id);
		//校验并获取标签
		var tag = this._getTag(tag_id);
		//商品删除标签
		goods._removeTag(tag_id);
		tag._removeGoods(goods_id);
	},
	hasTagById: function(tag_id) {
		var tags = this.tags || [];
		return tags.indexOf(tag_id) !== -1;
	}
};
module.exports = skill;

var db = require("../db/index");
var DomainCon = require("../domain/_con");
var GoodsCon = require("../goods/_con");
var UserCon = require("../../core/user/_con");
var BusCon = require("../../core/bus/_con");
var AchCon = require("../ach/_con");
var ClassifyCon = require("../bus.classify/_con");
var TagCon = require("../bus.tag/_con");
var LeagueCon = require("../league/_con");
var BuffCon = require("../buff/_con");
var BusUserBindCon = require("../bus_user_bind/_con");
var BusGuideCon = require("../bus.guide/_con");
var CommentCon = require("../comment/_con");
var config = require("../_include/config");
var data_cut = require("../_include/data_cut");
var helper = require("../_include/helper");
var md5 = require("../_include/md5");
var $Object = require("../_include/$.Object");
var $Array = require("../_include/$.Array");
var tld = require('tldjs');
var fs = require("fs");
var Fiber = require('fibers');
var LBS = require("../_include/baiduLBS");
var midEvent = require("../../middleware/index");